package com.wlw.eye.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.wlw.eye.R
import com.wlw.eye.adapter.RankAdapter
import com.wlw.eye.mvp.contract.HotContract
import com.wlw.eye.mvp.model.bean.HotBean
import com.wlw.eye.mvp.presenter.HotPresenter
import kotlinx.android.synthetic.main.fragment_rank.*

class RankFragment : BaseFragment(), HotContract.View {

    lateinit var mPresenter: HotPresenter
    lateinit var mStrategy: String
    lateinit var mAdapter: RankAdapter
    var mList: ArrayList<HotBean.ItemListBean.DataBean> = java.util.ArrayList()

    companion object {
        fun instance(strategy: String): RankFragment {
            val fragment = RankFragment()
            val bundle = Bundle()
            bundle.putString("strategy", strategy)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun setData(bean: HotBean) {
        if (mList.size > 0)
            mList.clear()
        bean.itemList?.forEach { it.data?.let { it1 -> mList.add(it1) } }
        mAdapter.notifyDataSetChanged()
    }

    override fun getLayoutResources(): Int = R.layout.fragment_rank

    override fun initView() {
        recyclerView.layoutManager = LinearLayoutManager(context)
        mAdapter = RankAdapter(mList)
        recyclerView.adapter = mAdapter
        mStrategy = arguments?.getString("strategy") ?: ""
        mPresenter = context?.let { HotPresenter(it, this) }!!
        mPresenter.requestData(mStrategy)
    }
}

